Member Function in C++ - Cyber Thieve

Member Function in C++ - Cyber Thieve


Member Function : 

Member Function is a function that has its definition declared in a class is called member function. member Function can be defined as public privet or protected visibility method.
If the member function is defined inside the class definition, it can be defined directly inside the class otherwise, we need to use the scope resolution operator (::) to declare the member function in C++ outside the class.



Also Read :
Advantages of Object Oriented Programming
Object and Classes in C++
C++ Object Oriented Programming


Member Function declaration :

class className{
/* Data Members
     .
             .
             .

*/
public:
// Member function 1
returnType1 functionName1(arguments1);

// Member function 2
returnType2 functionName2(arguments2){
/* Function Definition
                    .
                    .
                    .

*/
}
};

There are 2 ways to define a member function:

  • Inside class definition

class Student{
    name (string type)
    DOB (string type)
    rollNo (integer type)
    Year (integer type)
}

What Issue with inside class declaration : 

when member functions are defined within the class it makes the function inline and implicit expansion takes place, which ultimately reduces the performance of the code 

inline function results in larger code size so it may cause thrashing in memory. Also, more page fault occurs; hence, defining the function outside the class is recommended.

  • Outside class definition

class className{
public/private:
   returnType memberFunctionName (arguments);
};

returnType className :: memberFunctionName (arguments){
/* Statements
.
.
.
*/
}
main(){
   className object;

   object.memberFunctionName(arguments);
}

Class :

Class are a blueprint or a set of instructions to build a specific type of object. It is a basic concept of Object-Oriented Programming which revolve around the real-life entities.
Class determines how an object will behave and what the object will contain.

Members In Class : 

Class has two type of members in it.

  1. data members
  2. Member Function


People Also Search :

1. What are the types of member function?
  • Simple functions.
  • Static functions.
  • Const functions.
  • Inline functions.
  • Friend functions.
2. Why member functions are used?

The main aim of using the member function is to provide modularity to a program, which is generally used to improve code reusability and to make code maintainable.

3. What is the difference between function and member function in C++?

A normal function is always declared outside the class and a member function is always declared within the class (it can be defined outside the class)

4. What is inline function in C++?

Inline function in C++ is an enhancement feature that improves the execution time and speed of the program. The main advantage of inline functions is that you can use them with C++ classes as well.

Post a Comment

0 Comments
* Please Don't Spam Here. All the Comments are Reviewed by Admin.

#buttons=(Ok, Go it!) #days=(20)

Our website uses cookies to enhance your experience. Learn More
Ok, Go it!